﻿using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Text;

public class SerialReader : MonoBehaviour {

    SerialPort sp = new SerialPort("COM7", 9600);
    bool strike1 = false;

    public ObjectManager OM;

    public string currentString = "";
    public string completedString;
    int temps;
	// Use this for initialization
	void Start () {
        sp.Open();
        sp.ReadTimeout = 1;
	}
	
	// Update is called once per frame
	void Update () {


        if (sp.IsOpen)
        {
            try
            {
                temps = sp.ReadByte();
                if (temps != 10)
                {
                    char c = System.Convert.ToChar(temps);
                    currentString += c;
                }
                else
                {
                    processBytes(currentString);
                }
               //print(currentString);

                
            }
            catch (System.Exception)
            {
                if (currentString != "")
                {
                    processBytes(currentString);
                }
            }

        }


	}

    void processBytes(string t)
    {
        completedString = currentString.Trim();
        currentString = "";
        OM.checkString();
        print(completedString);

       
    }
}
